/*
 * Created by 岱青海蓝信息系统(北京)有限公司 on 17-7-4 下午5:41
 * Copyright (c) 2017. All rights reserved.
 */

package com.example.dqhl.jdmetro.activities;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.dqhl.jdmetro.R;
import com.example.dqhl.jdmetro.base.BaseActivity;
import com.example.dqhl.jdmetro.config.Constant;
import com.example.dqhl.jdmetro.listener.OnCommonConfirmListener;
import com.example.dqhl.jdmetro.utils.DataCleanManager;
import com.example.dqhl.jdmetro.utils.DeviceUtils;
import com.example.dqhl.jdmetro.utils.Dlog;
import com.example.dqhl.jdmetro.utils.JumpItent;
import com.example.dqhl.jdmetro.utils.Toastutils;
import com.example.dqhl.jdmetro.utils.UserSaveUtil;
import com.example.dqhl.jdmetro.view.WiperSwitch;
import com.example.dqhl.jdmetro.widget.CommonConfirmCancelDialog;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * description:设置
 * autour:guanluocang
 * date:17/7/6 10:56
 */
public class SettingActivity extends BaseActivity implements WiperSwitch.OnChangedListener {

    private Context context;
    @Bind(R.id.switch_msg_push)
    WiperSwitch switch_msg_push;//消息推送开关
    @Bind(R.id.tv_version)
    TextView tv_version;
    @Bind(R.id.rl_settingClear)
    RelativeLayout rl_settingClear;
    @Bind(R.id.tv_cache)
    TextView tv_cache;
    @Bind(R.id.rl_updataPassword)
    RelativeLayout rl_updataPassword;
    private String versionName = "";
    @Bind(R.id.tv_quit)
    TextView tv_quit;//退出登录
    private CommonConfirmCancelDialog commonConfirmCancelDialog;


    @Override
    public void initView(Bundle savedInstanceState) {
        context = this;
        setContentView(R.layout.activity_setting);
        ButterKnife.bind(this);
        initTitleBar(R.string.txt_setting);
        setRightTextVisibility(View.GONE);
    }

    @Override
    public void initData() {
        versionName = DeviceUtils.getVersionName(context);
        tv_version.setText(versionName);
        tv_cache.setText(DataCleanManager.getTotalCacheSize(context));
        switch_msg_push.setChecked(false);
    }

    @Override
    public void initListener() {
        switch_msg_push.setOnChangedListener(this);
        rl_settingClear.setOnClickListener(this);
        rl_updataPassword.setOnClickListener(this);
        tv_quit.setOnClickListener(this);
    }

    @Override
    public void OnChanged(WiperSwitch wiperSwitch, boolean checkState) {
        Dlog.e("开关: " + checkState);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //清除缓存
            case R.id.rl_settingClear:
                onClearCache();
                break;
            //修改密码
            case R.id.rl_updataPassword:
                if (user == null) {
                    Toastutils.show("请登录");
                } else {
                    JumpItent.jump(SettingActivity.this, UpdataPasswordActivity.class);
                }
                break;
            case R.id.tv_quit:
                if (user == null) {
                    Toastutils.show("暂未登录");
                } else {
                    doLogout();
                }
                break;
        }
    }

    /**
     * 退出登录
     */
    private void doLogout() {
        if (commonConfirmCancelDialog == null) {
            commonConfirmCancelDialog = new CommonConfirmCancelDialog(context, "您确定要退出?",
                    new OnCommonConfirmListener() {
                        @Override
                        public void onConfirm() {
                            user = null;
                            UserSaveUtil.saveUser(context, user);
                            // TODO: 16/11/25  发送退出登录的广播
                            Intent intent = new Intent(Constant.ACTION_LOGOUT_SUCCESS);
                            sendBroadcast(intent);
                            finish();

                        }
                    });
        }
        commonConfirmCancelDialog.show();
    }

    /**
     * 清除缓存
     */

    private void onClearCache() {
        DataCleanManager.clearAllCache(context);
        tv_cache.setText(DataCleanManager.getTotalCacheSize(context));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }
}
